import { WeatherApi } from '../../constant/API';
import { WeatherForNow } from './dto/WeatherForNow';
import { WeatherForFutureHour } from './dto/WeatherForFutureHour';
import { Daily, WeatherForFutureDay } from './dto/WeatherForFutureDay';
import ArrayList from '@ohos.util.ArrayList';
import http from '@ohos.net.http';
import { WeatherIndex } from './dto/WeatherIndex';
import { LiveData } from '@feature/basic_service/src/main/ets/tools/LiveData';
import { DataStore } from '@feature/basic_service/src/main/ets/tools/DataStore';
import { preKey } from '@feature/basic_service/src/main/ets/BasicConstant';
import { Http } from '@feature/basic_service/src/main/ets/tools/Http';

export class WeatherModel {
  //计算温度进度条
  tempOffset: ArrayList<number> = new ArrayList()
  //计算温度的展示高度
  tempLength: ArrayList<number> = new ArrayList()
  //当前城市
  private currentCityId: string = "101010100"
  private currentCityName: string = "北京"

  //数据
  nowDto = new LiveData<WeatherForNow>()
  futureHourDto = new LiveData<WeatherForFutureHour>()
  futureDayDto = new LiveData<WeatherForFutureDay>()
  weatherIndex = new LiveData<WeatherIndex>()
  currentCity = new LiveData<string>()

  /**
   * 初始化
   */
  getWeatherData() {

    DataStore.instance.getString(preKey.cityName).then((cityName) => {
      if (cityName.length > 0) {
        this.currentCityName = cityName
      } else {
        this.currentCityName = "北京"
      }
      this.currentCity.setValue(this.currentCityName)
    })

    DataStore.instance.getString(preKey.cityId).then((cityId) => {
      if (cityId.length > 0) {
        this.currentCityId = cityId
      } else {
        this.currentCityId = "101010100"
      }
      //获取当前天气
      this.getWeatherForNow()
        .then((dto) => {
          this.nowDto.setValue(dto)
        })
      //获取未来24小时天气
      this.getWeatherForHour()
        .then((dto) => {
          this.futureHourDto.setValue(dto)
        })
      //获取未来7天的天气
      this.getWeatherForDay()
        .then((dto) => {
          this.countTemp(dto)
          this.futureDayDto.setValue(dto)
        })
      //获取天气指数
      this.getWeatherIndex()
        .then((dto) => {
          this.weatherIndex.setValue(dto)
        })
    })
  }

  /**
   * 切换城市
   * @param cityId
   * @param cityName
   */
  async cutCity(cityId: string, cityName: string) {
    //记录城市数据
    await DataStore.instance.put(preKey.cityId, cityId)
    await DataStore.instance.put(preKey.cityName, cityName)

    //获取天气数据
    this.getWeatherData()
  }

  /**
   * 获取当前城市的当前天气
   */
  private async getWeatherForNow(): Promise<WeatherForNow> {
    let response = await Http.getSync(WeatherApi.getWeatherForNowApi(this.currentCityId))
    return response.responseCode == http.ResponseCode.OK ? WeatherForNow.transition(response.result) : null
  }

  /**
   * 获取当前城市的小时天气
   */
  private async getWeatherForHour(): Promise<WeatherForFutureHour> {
    let response = await Http.getSync(WeatherApi.getWeatherForHourApi(this.currentCityId))
    return response.responseCode == http.ResponseCode.OK ? WeatherForFutureHour.transition(response.result) : null
  }

  /**
   * 获取当前城市的未来几天的天气
   */
  private async getWeatherForDay(): Promise<WeatherForFutureDay> {
    let response = await Http.getSync(WeatherApi.getWeatherForDayApi(this.currentCityId))
    return response.responseCode == http.ResponseCode.OK ? WeatherForFutureDay.transition(response.result) : null
  }

  /**
   * 获取天气指数
   */
  private async getWeatherIndex(): Promise<WeatherIndex> {
    let response = await Http.getSync(WeatherApi.getWeatherIndexApi(this.currentCityId))
    return response.responseCode == http.ResponseCode.OK ? WeatherIndex.transition(response.result) : null
  }

  /**
   * 计算温度进度条展示数据
   */
  private countTemp(dto: WeatherForFutureDay) {
    let minTemp: number = null
    let maxTemp: number = null
    //计算最大温度和最小温度
    dto.daily.forEach((daily: Daily) => {
      if (minTemp == null) {
        minTemp = parseInt(daily.tempMin)
      } else if (minTemp > parseInt(daily.tempMin)) {
        minTemp = parseInt(daily.tempMin)
      }
      if (maxTemp == null) {
        maxTemp = parseInt(daily.tempMax)
      } else if (maxTemp < parseInt(daily.tempMax)) {
        maxTemp = parseInt(daily.tempMax)
      }
    })


    //计算偏移量
    dto.daily.forEach((daily: Daily) => {
      let dailyMinTemp: number = parseInt(daily.tempMin)
      let dailyMaxTemp: number = parseInt(daily.tempMax)
      let length = (dailyMaxTemp - dailyMinTemp) / (maxTemp - minTemp) * 100
      let offset = (maxTemp - dailyMaxTemp) * (100 / (maxTemp - minTemp))

      this.tempOffset.add(offset)
      this.tempLength.add(length)
    })
  }
}